package educar.controllers;


import educar.gui.IView;
import educar.gui.admin.AddUser;
import educar.idioma.Locale;
import educar.models.User;

/**
 * Permite agregar un usuario
 */
public class AddUserController implements IController {
	private AddUser view;
	
	public void process(String model){
		if(null!=view) {
			/*obtengo datos y compruebo validez*/
			String name = view.getUsername();
			String password = view.getPassword();
			String role = view.getRole();
			if(name.length()==0 ||password.length()==0 || role.length()==0) {
				view.presentWarning(Locale.EMPTY_FIELDS, Locale.ERROR);
			}
			else {
				/*intento crear el usuario*/
				User newuser = new User("0",name,password,role);
				boolean result = newuser.save();
				if (!result) {
					view.presentWarning(Locale.USER_EXIST,Locale.ERROR);
				}
				else {
					view.present(Locale.USERS+" "+Locale.SAVED);
					view.close();	
				}
			}
		}	
	}
	
	
	public void setView(IView view) {
		this.view = (AddUser) view;	
	}
}
